package com.example.cxx.myapplication;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ExpandableListView;
import android.widget.TextView;

import java.util.ArrayList;
import java.util.List;


public class MainActivity extends Activity {

    private AnimatedExpandableListView listView;
    private ExampleAdapter adapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        List<GroupItem> items = new ArrayList<GroupItem>();

        // Populate our list with groups and it's children
        for (int i = 1; i < 10; i++) {
            GroupItem item = new GroupItem();

            item.title = "Group " + i;

            for (int j = 0; j < 3; j++) {
                ChildItem child = new ChildItem();
                child.title = "Awesome item " + j;
                child.hint = "Too awesome";

                item.items.add(child);
            }

            items.add(item);
        }

        Button button = (Button) findViewById(R.id.button);
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startActivity(new Intent(MainActivity.this, NextActivity.class));

                overridePendingTransition(R.anim.left, R.anim.right);
            }
        });

        adapter = new ExampleAdapter(this);
        adapter.setData(items);

        listView = (AnimatedExpandableListView) findViewById(R.id.listView);
        listView.setAdapter(adapter);

        // In order to show animations, we need to use a custom click handler
        // for our ExpandableListView.
        listView.setOnGroupClickListener(new ExpandableListView.OnGroupClickListener() {

            @Override
            public boolean onGroupClick(ExpandableListView parent, View v, int groupPosition, long id) {
                // We call collapseGroupWithAnimation(int) and
                // expandGroupWithAnimation(int) to animate group
                // expansion/collapse.
                if (listView.isGroupExpanded(groupPosition)) {
                    listView.collapseGroupWithAnimation(groupPosition);
                } else {
                    listView.expandGroupWithAnimation(groupPosition);
                }
                return true;
            }

        });
        listView.setGroupIndicator(null);
    }

    private static class GroupItem {
        String title;
        List<ChildItem> items = new ArrayList<ChildItem>();

        public String getTitle() {
            return title;
        }
    }

    private static class ChildItem {
        String title;
        String hint;
    }

    private static class ChildHolder {
        TextView title;
        TextView hint;
    }

    private static class GroupHolder {
        TextView title;
    }

    /**
     * Adapter for our list of {@link GroupItem}s.
     */
    private class ExampleAdapter extends AnimatedExpandableListView.AnimatedExpandableListAdapter {
        private LayoutInflater inflater;

        private List<GroupItem> items;

        public ExampleAdapter(Context context) {
            inflater = LayoutInflater.from(context);
        }

        public void setData(List<GroupItem> items) {
            this.items = items;
        }

        @Override
        public ChildItem getChild(int groupPosition, int childPosition) {
            return items.get(groupPosition).items.get(childPosition);
        }

        @Override
        public long getChildId(int groupPosition, int childPosition) {
            return childPosition;
        }

        @Override
        public View getRealChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {
            ChildHolder holder;
            ChildItem item = getChild(groupPosition, childPosition);
            if (convertView == null) {
                holder = new ChildHolder();
                convertView = inflater.inflate(R.layout.list_item, parent, false);
                holder.title = (TextView) convertView.findViewById(R.id.textTitle);
                holder.hint = (TextView) convertView.findViewById(R.id.textHint);
                convertView.setTag(holder);
            } else {
                holder = (ChildHolder) convertView.getTag();
            }

            holder.title.setText(item.title);
            holder.hint.setText(item.hint);


            return convertView;
        }

        @Override
        public int getRealChildrenCount(int groupPosition) {
            return items.get(groupPosition).items.size();
        }

        @Override
        public GroupItem getGroup(int groupPosition) {
            return items.get(groupPosition);
        }

        @Override
        public int getGroupCount() {
            return items.size();
        }

        @Override
        public long getGroupId(int groupPosition) {
            return groupPosition;
        }

        @Override
        public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {
            GroupHolder holder;
            GroupItem item = getGroup(groupPosition);
            if (convertView == null) {
                holder = new GroupHolder();
                convertView = inflater.inflate(R.layout.group_item, null);
                holder.title = (TextView) convertView.findViewById(R.id.textTitle);
                convertView.setTag(holder);
            } else {
                holder = (GroupHolder) convertView.getTag();
            }

            holder.title.setText(item.title);

            return convertView;
        }

        @Override
        public boolean hasStableIds() {
            return true;
        }

        @Override
        public boolean isChildSelectable(int arg0, int arg1) {
            return true;
        }

    }


}


//
//import android.app.Activity;
//import android.content.Context;
//import android.graphics.Bitmap;
//import android.graphics.BitmapFactory;
//import android.graphics.Canvas;
//import android.graphics.Color;
//import android.graphics.Paint;
//import android.graphics.PorterDuff;
//import android.graphics.PorterDuff.Mode;
//import android.graphics.PorterDuffXfermode;
//import android.graphics.Rect;
//import android.graphics.RectF;
//import android.graphics.drawable.Drawable;
//import android.os.Bundle;
//import android.view.View;
//
//public class MainActivity extends Activity {
//    @Override
//    public void onCreate(Bundle savedInstanceState) {
//        super.onCreate(savedInstanceState);
//        setContentView(new ImageEffect(this));
//    }
//
//    class ImageEffect extends View {
//        Paint paint;
//
//        public ImageEffect(Context context) {
//            super(context);
//            paint = new Paint();//初始化画笔，为后面阴影效果使用。
//            paint.setAntiAlias(true);//去除锯齿。
//            paint.setShadowLayer(5f, 5.0f, 5.0f, Color.BLACK);//设置阴影层，这是关键。
//            paint.setXfermode(new PorterDuffXfermode(Mode.SRC_IN));
//        }
//
//        @Override
//        public void onDraw(Canvas canvas) {
//            super.onDraw(canvas);
//            int posX = 20;
//            int posY = 50;
//            int PicWidth, PicHegiht;
//            Drawable drawable = getResources().getDrawable(R.drawable.female);
//            Drawable dbe = getResources().getDrawable(R.drawable.female).mutate();//如果不调用mutate方法，则原图也会被改变，因为调用的资源是同一个，所有对象是共享状态的。
////            Drawable drawTest = getResources().getDrawable(R.drawable.female);
//            Bitmap bmp = BitmapFactory.decodeResource(getResources(), R.drawable.female);
//            PicWidth = drawable.getIntrinsicWidth();
//            PicHegiht = drawable.getIntrinsicHeight();
////            drawTest.setBounds(posX, (2 * posY) + PicHegiht, posX + PicWidth, (2 * posY) + 2 * PicHegiht);
//            drawable.setBounds(posX, posY, posX + PicWidth, posY + PicHegiht);
//            dbe.setBounds(0, 0, PicWidth, PicHegiht);
//            canvas.drawColor(Color.WHITE);//设置画布颜色
//            canvas.save(Canvas.MATRIX_SAVE_FLAG);
//            dbe.setColorFilter(0x7f000000, PorterDuff.Mode.SRC_IN);
//            canvas.translate(posX + (int) (0.9 * PicWidth / 2), posY + PicHegiht / 2);//图像平移为了刚好在原图后形成影子效果。
//            canvas.skew(-0.9F, 0.0F);//图像倾斜效果。
//            canvas.scale(1.0f, 0.5f);//图像（其实是画布）缩放，Y方向缩小为1/2。
//            dbe.draw(canvas);//此处为画原图像影子效果图，比原图先画，则会在下层。
//            drawable.clearColorFilter();
//            canvas.restore();
//            canvas.save(Canvas.MATRIX_SAVE_FLAG);
//            drawable.draw(canvas);//此处为画原图像，由于canvas有层次效果，因此会盖在影子之上。
//            canvas.restore();
//            //默认无效果原图
//            canvas.save(Canvas.MATRIX_SAVE_FLAG);
////            drawTest.draw(canvas);
//            canvas.restore();
//            //图片阴影效果
//            canvas.save(Canvas.MATRIX_SAVE_FLAG);
//            //Rect rect = new Rect(2*posX + PicWidth, 2*posY + PicHegiht, 2*posX + 2*PicWidth, 2*posY + 2*PicHegiht);//此为理论上的阴影图坐标
//            Rect rect = new Rect(2 * posX + PicWidth + 3, 2 * posY + PicHegiht + 3, 2 * posX + 2 * PicWidth - 2, 2 * posY + 2 * PicHegiht - 2);
//            //由于图片的实际尺寸比显示出来的图像要大一些，因此需要适当更改下大小，以达到较好的效果
//            RectF rectF = new RectF(rect);
//            canvas.drawRoundRect(rectF, 10f, 10f, paint);//在原有矩形基础上，画成圆角矩形，同时带有阴影层。
//            canvas.drawBitmap(bmp, 2 * posX + PicWidth, 2 * posY + PicHegiht, null);//画上原图。
//            canvas.restore();
//        }
//    }
//}
